package submit;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import db.Db;
import db.DeltaRreg;
import db.Preg;
import db.WellReg;
import db.skriveTilDb;
import wellsim.no.ntnu.fp.su.Flow;
import wellsim.no.ntnu.fp.su.Well;

public class AlarmTest {
	
	private static Db  dbm = Db.getInstance();
	
	private double EV;
	private double deltaTime = 1.0;
	private String Alarm = "Feil";
	private double EVlim;
	
	public void checkColor(){
		//if(System.currentTimeMillis() % 1000 == 0) break;
		
		
	}
	
	
	
	
	private static List<DeltaRreg> deltaR() throws SQLException {
		
		List<DeltaRreg> liste = new ArrayList<DeltaRreg>();
		
		String query = "SELECT deltaR FROM DeltaR ";
		ResultSet rs = dbm.executeQuery(query);
		while(rs.next()){
			double deltaR = rs.getDouble(1);
			
			liste.add(new DeltaRreg(deltaR));
		}
		System.out.println(liste);
		return liste;
		
		
	}

	public void eVAlarm() throws SQLException{
		List <DeltaRreg> resultat = deltaR();
		double x = resultat.get(resultat.size()-1).getDeltaR();
		EV = x/deltaTime; //REGNER UT EV
		EVlim = EV/deltaTime; //REGNER UT EVlim
		
		double minTemperature = Flow.minTemperature;
		double maxTemperature = Flow.maxTemperature;
		double deltaTemperature = maxTemperature - minTemperature;
		
		double minPressure = Flow.minPressure;
		double maxPressure = Flow.maxPressure;
		double deltaPressure = maxPressure - minPressure;
		if(deltaTemperature > deltaPressure){
			System.out.println(Alarm);
		}
		System.out.println(x);
	}

	private static List<Preg> getDeltaP()throws SQLException {
	List<Preg> listeP = new ArrayList<Preg>();
		
		String query = "SELECT P1 FROM SampleWell ";
		ResultSet rs = dbm.executeQuery(query);
		while(rs.next()){
			double P1 = rs.getDouble(1);
			
			listeP.add(new Preg(P1));
		}
		System.out.println(listeP);
		
		double x = listeP.get(listeP.size()-1).getP1();
		double y = listeP.get(listeP.size()-2).getP1();
		
		System.out.println(x);
		System.out.println(y);
		
		double deltaP = y-x;
		
		return listeP;
	}
	
	public void pressureTempratureAlarm(Flow flow) throws SQLException{
		//Denne skal sjekke om deltaP/deltaT>deltaP limit-2
		List <Preg> resultat = getDeltaP();
		double x = resultat.get(resultat.size()-1).getP1();
		double y = resultat.get(resultat.size()-2).getP1();
		
		System.out.println(x);
		System.out.println(y);
		
		double minPressure = Flow.minPressure;
		double maxPressure = Flow.maxPressure;
		
		double deltaP = 0;
		double deltaP2 = (maxPressure-minPressure)/ deltaTime;
		double pOvert = deltaP/deltaTime;
		
		if(pOvert > deltaP2){
			System.out.println(Alarm);
		}
	}
	
	public void pressureAlarm(Flow flow){
		double pressure = flow.getPressure();
		double maxPressure = Flow.maxPressure;
		
		if(pressure > maxPressure){
			System.out.println(Alarm);
		}
	}
	public static void main(String [] args) throws SQLException{
//		deltaR();
		getDeltaP();
//		new AlarmTest().eVAlarm();

	}
}
